package domain.model;

import java.util.ArrayList;
import java.util.List;

import domain.SpelerTekenChar;
import domain.iObserver;
import domain.iSpelerTeken;
import domain.exceptions.DomainException;

public class TicTacToeGame implements iGame {
	private Board board;
	private iSpelerTeken[] signs = { new SpelerTekenChar('O'), new SpelerTekenChar('X') };
	private String titel;
	private int currentSign = 0;
	private List<iObserver> observers = new ArrayList<iObserver>();

	public TicTacToeGame( ) {
		board = new Board(3,3, new SpelerTekenChar());
		setTitel("TicTacToe");
	}

	@Override
	public void play(int row, int col) throws DomainException {
		// check if valid move
		board.set(row, col, getSign());

		// check if game over
		moveToNextSign();
		notifyObservers();
	}
		
	
	private iSpelerTeken getSign() {
		return signs[currentSign];
	}

	@Override
	public void setSigns(iSpelerTeken s1, iSpelerTeken s2) {
		iSpelerTeken[] hulp = { s1, s2 };
		this.signs = hulp;
	}

	private void moveToNextSign() {
		currentSign++;
		currentSign = currentSign % 2;
	}

	@Override
	public Board getBoard() {
		return board;
	}

	private void setTitel(String titel) {
		this.titel = titel;
	}

	@Override
	public String getTitel() {
		return titel;
	}

	@Override
	public void notifyObservers() {
		for (iObserver o : observers) {
			o.update(this);
		}
	}

	@Override
	public void registerObserver(iObserver obs) {
		observers.add(obs);
	}

	@Override
	public void removeObserver(iObserver obs) {
		observers.remove(obs);
	}

	@Override
	public void setSigns(iSpelerTeken iSpelerTeken) {
		signs[1] = iSpelerTeken;
		
	}

}